<template>
  <div class="page-style">
    <storehouse-btn :warehouse-list="warehouseInfoList" @downloadFun="exportExcel" @searchFun="searchFun" @reloadFun="reloadFun" />
    <storehouse-tab :storehouse-list="storehouseList" :warehouse-list="warehouseInfoList" @reloadFun="reloadFun" />
    <pagination :total="total" @pagination="pagination" />
  </div>
</template>

<script>
import storehouseBtn from './components/storehouse/storehouse-btn'
import storehouseTab from './components/storehouse/storehouse-tab'
import pagination from '@/components/Pagination'
import { getStorehouseInfo, getWarehouseInfo } from '@/api/wms'

export default {
  components: { storehouseBtn, pagination, storehouseTab },
  data() {
    return {
      total: 0,
      storehouseList: [],
      page: 1,
      num: 10,
      warehouse_id: '',
      storehouse_name: '',
      warehouseInfoList: []
    }
  },
  created() {
    this.getStorehouseInfo()
    this.getWarehouseInfoList()
  },
  methods: {
    // 获取库位
    getStorehouseInfo() {
      const data = {
        p: this.page,
        num: this.num,
        warehouse_id: this.warehouse_id,
        storehouse_name: this.storehouse_name
      }
      getStorehouseInfo(data).then(res => {
        console.log(res)
        this.storehouseList = res.list
        this.total = Number(res.count)
      })
    },
    // 获取仓库
    getWarehouseInfoList() {
      getWarehouseInfo().then(res => {
        console.log(res)
        this.warehouseInfoList = res.list
      })
    },
    // 检索
    searchFun(val) {
      console.log(val)
      this.warehouse_id = val.warehouseId
      this.storehouse_name = val.name
      this.getStorehouseInfo()
    },
    // 分页
    pagination(val) {
      this.page = val.page
      this.num = val.limit
      this.getStorehouseInfo()
    },
    reloadFun() {
      this.getStorehouseInfo()
    },
    // 导出表格
    exportExcel() {
      const filename = '库位信息表格'
      const header = ['仓库名称', '库位名称', '名称描述', '库存能力']
      const data = []
      const list = this.storehouseList
      for (const i in list) {
        const arr = []
        arr.push(list[i].warehouse_content.warehouse_name, list[i].storehouse_name, list[i].desc, ' ' + list[i].inventory)
        data.push(arr)
      }
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          filename: filename,
          data: data,
          autoWidth: true,
          header: header
        })
      })
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
